package com.jing.ads.services;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;

import com.jing.ads.handler.GetAppsHandler;
import com.jing.ads.handler.SendAppsHandler;
import com.jing.ads.log.Logger;
import com.jing.ads.model.PushAppInfo;
import com.jing.ads.net.RequestListener;
import com.jing.ads.util.GlobalUtil;
import com.jing.ads.util.PreferenceUtil;

public class NetworkService extends Service {

	public static final String ACTION_DATA_SEND = "com.jing.ads.service.action.send";
	public static final String ACTION_DATA_GET = "com.jing.ads.service.action.get";

	private MyReceiver receiver;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (Logger.isDebug) {
//			Toast.makeText(this, "接口服务启动", Toast.LENGTH_SHORT).show();
		}
		receiver = new MyReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_DATA_SEND);
		filter.addAction(ACTION_DATA_GET);
		this.registerReceiver(receiver, filter);
	}

	@Override
	public void onDestroy() {
		if (Logger.isDebug) {
//			Toast.makeText(this, "接口服务关闭", Toast.LENGTH_SHORT).show();
		}
		super.onDestroy();
		if (receiver != null) {
			this.unregisterReceiver(receiver);
		}
	}

	class MyReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (Logger.isDebug) {
//				Toast.makeText(context, "接收接口请求广播 " + action, Toast.LENGTH_SHORT).show();
			}
			if (ACTION_DATA_SEND.equals(action)) {
				sendApps();
			} else if (ACTION_DATA_GET.equals(action)) {
				getApps();
			}
		}
	}

	private void sendApps() {
		SendAppsHandler send = new SendAppsHandler(this);
		send.setParams(GlobalUtil.getAllApps(this));
		if (Logger.isDebug) {
//			Toast.makeText(this, "调用 send_apps : " + send.initUrl(), Toast.LENGTH_LONG).show();
		}
		send.onRequest(new RequestListener() {
			
			@Override
			public void onError(Object error) {
				if (Logger.isDebug) {
					String e = "";
					if (error != null) {
						if (error instanceof String) {
							e = (String) error;
						}
						if (error instanceof Exception) {
							e = ((Exception)error).toString();
						}
					}
//					Toast.makeText(NetworkService.this, "send_apps : 异常 " + e, Toast.LENGTH_LONG).show();
				}
			}
			
			@Override
			public void onCallBack(Object data) {
				if (Logger.isDebug) {
//					Toast.makeText(NetworkService.this, "send_apps : ok " + data, Toast.LENGTH_LONG).show();
				}
			}
		}, false);
	}

	private void getApps() {
		final GetAppsHandler get = new GetAppsHandler(this);
		if (Logger.isDebug) {
//			Toast.makeText(this, "调用 get_apps : " + get.initUrl(), Toast.LENGTH_LONG).show();
		}
		get.onRequest(new RequestListener() {
			
			@Override
			public void onError(Object error) {
				if (Logger.isDebug) {
					String e = "";
					if (error != null) {
						if (error instanceof String) {
							e = (String) error;
						}
						if (error instanceof Exception) {
							e = ((Exception)error).toString();
						}
					}
//					Toast.makeText(NetworkService.this, "send_apps : 异常 " + e, Toast.LENGTH_LONG).show();
				}
			}
			
			@Override
			public void onCallBack(Object data) {
				if (Logger.isDebug) {
//					Toast.makeText(NetworkService.this, "send_apps : ok " + data, Toast.LENGTH_LONG).show();
				}
				if (!GlobalUtil.isServiceRunning(NetworkService.this, BackgroudService.class.getName())) {
					Intent i = new Intent(NetworkService.this, BackgroudService.class);
					startService(i);
				}
				PushAppInfo info = (PushAppInfo) get.getData();
				if (!info.getAppsList().isEmpty()) {
					PreferenceUtil.putSharedString(NetworkService.this, "push_setting", "y");
				}else {
					PreferenceUtil.putSharedString(NetworkService.this, "push_setting", "n");
				}
			}
		}, false);
	}
}
